package com.three.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import net.sf.json.JSONObject;

import com.three.dao.UserDao;
import com.three.entity.User;
import com.three.service.UserService;
import com.three.util.Constant;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	UserDao userDao;
	
	@Override
	public JSONObject login(User user) {
		String code;
		JSONObject obj = new JSONObject();
		User u = userDao.findUserByPhone(user.getPhone());
		if (u == null) {
			code=Constant.LOGIN_ERROR_NOT_REG; //手机号未注册
			obj.put("code", code);
		}else{
			if (user.getPwd().equals(u.getPwd())) {
				code=Constant.LOGIN_SUCCESS; //登录成功
				obj.put("code", code);
				obj.put("user", u);
			}else{
				code=Constant.LOGIN_ERROR_PWD; //密码错误
				obj.put("code", code);
			}
		}
		return obj;
	}

	@Override
	public User findUserWithClassmates(String phone) {
		User user = new User();
		user = userDao.findUserByPhoneWithClassmates(phone);
		return user;
	}
	
	@Override
	public String register(User user) {
		int result=userDao.insertUser(user);
		if(result==1){
			return "regSuccess";
		}else{
			return "regfailed";
		}
		
	}

	@Override
	public int changeUserName(String name, String id) {
		int result=userDao.changeUserName(name, id);
		return result;
	}


	@Override
	public int changeCampus(String campus, String id) {	
		return userDao.changeCampus(campus, id);
	}
	
	@Override
	public int changeSno(String sno, String id) {	
		return userDao.changeSno(sno, id);
	}

	@Override
	public int changePhone(String phone, String id) {
		return userDao.changePhone(phone, id);
	}
	
	@Override
	public int checkPhone(String phone) {
		User user=userDao.findUserByPhone(phone);
		if(user==null){
			return 0;
		}
		return 1;
	}

	@Override
	public List<User> findUsersByCid(Integer cid) {
		return userDao.findUsersByCid(cid);
	}

	@Override
	public List<User> findStudentsByCid(Integer cid) {
		return userDao.findStudentsByCid(cid);
	}

	@Override
	public String changePwd(String oPwd, String uPwd, Integer uid) {
		String result = null;
		String pwd = userDao.findPwdByUid(uid);
		if (pwd.equals(oPwd)) {
			//原始密码正确,修改密码
			int state = userDao.updatePwd(uPwd, uid);
			if (state > 0) {
				result = "success";
			}else{
				result="failed";
			}
		
		}else{
			//原始密码错误
			result = "oPwdError";
		}
		return result;
	}

	@Override
	public String uploadIcon(HttpServletRequest request,MultipartFile file,Integer uid) {
		String result = null;
		String root = request.getRealPath(File.separator);
		root+=File.separator+"icons"+File.separator+file.getOriginalFilename();
		try {
			file.transferTo(new File(root));
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String picUrl = userDao.findPicUrlByUid(uid);
		int state = userDao.updatePicUrl(uid,file.getOriginalFilename());
		if (state>0) {
			if (!"init".equals(picUrl)) {
				File f = new File(request.getRealPath(File.separator)
						+File.separator+"icons"+File.separator+picUrl);
				f.delete();
			}
			result = "success";
		}else{
			result = "failed";
		}
		return result;
	}

}
